package com.tj.controller;

import com.tj.pojo.Doctor;
import com.tj.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/doc")
public class DoctorController {

    @Autowired
    DoctorService doctorService;

    // ================================= 查询用户 =================================
    /*
     * -取得用户列表数据
     */
    @RequestMapping("/getDoctorList")
    public Map<String, Object> getDoctorList() throws Exception {
        List<Doctor> doctorList = doctorService.findAllList();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", "1");
        map.put("doctorList",doctorList);
        return map;
    }

    /*
     * -按姓名进行模糊查找
     */
    @RequestMapping("/getDocListByName")
    public Map<String, Object> getDocListByName(@RequestParam String realName) throws Exception {
        List<Doctor> doctorList = doctorService.findDocListByName(realName);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", "1");
        map.put("doctorList", doctorList);
        return map;
    }

    /*
     * -按ID查
     */
    @RequestMapping("/getDocById")
    public Map<String, Object> getDocById(@RequestParam int docId) throws Exception {
        Doctor doctor = doctorService.findDocById(docId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", "1");
        map.put("doctor",doctor);
        return map;
    }

    @RequestMapping("doctor/getDoctorByCodeByPass")
    public Doctor getDoctorByCodeByPass(@RequestBody Doctor doctor) {
        return doctorService.login(doctor);
    }
    // ================================= 增加用户 =================================
    @RequestMapping("/insertDoctor")
    public int insertDoctor(@RequestBody Doctor doctor) {
        return doctorService.insertDoctor(doctor);
    }
    // ================================= 修改用户 =================================
    @RequestMapping("/updateDoctor")
    public int updateDoctor(@RequestBody Doctor doctor) {
        return doctorService.updateDoctor(doctor);
    }
    // ================================= 删除用户 =================================
    /*
     * -执行用户删除的操作
     */
    @RequestMapping("/doDelete")
    public Map<String, Object> doDelete(HttpSession session,Doctor doctor) throws Exception {
        int num = doctorService.deleteDoctor(doctor);
        Map<String, Object> map = new HashMap<String, Object>();
        if (num > 0) {
            map.put("status", "1");
        } else {
            map.put("status", "0");
        }
        return map;
    }

}
